<?php
class Model_Core_Resource_Collection_Abstract
    extends Zend_Db_Table_Abstract
{
    protected $_nameModel;

    protected $_items = array();

    protected $_page = 1;
 
    protected $_pageSize = null;

    protected $_filter = array();

    protected function _init($model, $tableName, $primary)
    {
        $this->_nameModel = $model;
        $this->_name = $tableName;
        $this->_primary = $primary;

        return $this;
    }

    public function load()
    {
        $select = $this->getAdapter()->select()
                        ->from($this->_name);
        if(isset($this->_pageSize)) {
            $select->limit($this->_pageSize,$this->_page);
        }

        $result = $this->getAdapter()->query($select);
        $result = $result->fetchAll(Zend_Db::FETCH_OBJ);

        foreach ($result as $_item) {
            $model = new $this->_nameModel();
            $model->setPrimary($this->_primary);
            $model->setOriginalData((array)$_item);
            $this->_items[] = $model;
        }

        return $this;
    }

    public function getItems()
    {
        return $this->_items;
    }

    public function getFirstItem()
    {
        $select = $this->getAdapter()->select()
                        ->from($this->_name);
        //$select->limit(1,$this->_page);

        $result = $this->getAdapter()->query($select);
        $result = $result->fetchAll(Zend_Db::FETCH_OBJ);

        $model = new $this->_nameModel();
        $model->setPrimary($this->_primary);
        $model->setOriginalData((array)$result[0]);

        return $model;
    }

    public function setPage($page, $pageSize = null)
    {
        $this->_page = $page;
        $this->_pageSize = $pageSize;

        return $this;
    }

    public function addFieldToFilter($field,$condition)
    {
        $this->_filter[$field] = $condition;
        return $this;
    }
}